package com.fita.admin.service.authority.impl;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.fita.admin.dao.ResourceDao;
import com.fita.admin.dao.RoleDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.authority.RoleService;
import com.fita.entity.authority.Role;
import com.fita.entity.authority.RoleTemplate;
import com.fita.entity.principal.Principal;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 * @author Ark
 *
 */
@Service("roleService")
public class RoleServiceImpl extends DataServiceImpl<Role> implements RoleService {

    @Dao
    private RoleDao roleDao;
    
    @Dao
    private ResourceDao resourceDao;
    
    /* (non-Javadoc)
     * @see com.unben.admin.service.RoleService#count(com.unben.entity.authority.RoleTemplate)
     */
    public int count(RoleTemplate roleTemplate) {
        return roleDao.count(roleTemplate);
    }
    
    /* (non-Javadoc)
     * @see com.unben.admin.service.RoleService#list()
     */
    public Map<String, Role> list() {
        List<Role> roles = this.listAll(Role.class);
        Map<String, Role> roleMaps = CollectionUtils.constructMapValue(roles, "code");
        return roleMaps;
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.RoleService#load(com.fita.entity.principal.Principal)
     */
    public Role loadRole(Principal principal) {
        return roleDao.loadByCode(principal.getAuthority());
    }

}
